import { Injectable,EventEmitter } from '@angular/core';

import { Http } from '@angular/http';
import { Service } from '../../server/server';
import { tablestructureDatas } from './database.model';

@Injectable()
export class DatabaseService {

  constructor(private http: Http, private server: Service) {
    this.typeDate();
    this.modelDate = {
      name:"",
      id:"",
    }
    this.databaseAddeventEmit = new EventEmitter();
  }

  public databaseAddeventEmit:any;//门户端删除状态

  public modelDate:any;

  public typeDate(){
    this.typelist = [
      { name: 'int' },
      { name: 'integer' },
      { name: 'decimal' },
      { name: 'char' },
      { name: 'varchar' },
      { name: 'date' },
      { name: 'text' },
      { name: 'mediumtext' },
      { name: 'longtext' },
      // { name: 'tinyint', },
      // { name: 'smallint' },
      // { name: 'mediumint' },
      // { name: 'bigint' },
      // { name: 'bit' },
      // { name: 'real' },
      // { name: 'double' },
      // { name: 'float' },
      // { name: 'numeric' },
      // { name: 'time' },
      // { name: 'year' },
      // { name: 'timestamp' },
      // { name: 'datetime' },
      // { name: 'tinyblob' },
      // { name: 'blob' },
      // { name: 'mediumblob' },
      // { name: 'longblob' },
      // { name: 'tinytext' },
      // { name: 'enum' },
      // { name: 'set' },
      // { name: 'binary' },
      // { name: 'varbinary' },
      // { name: 'point' },
      // { name: 'linestring' },
      // { name: 'polygon' },
      // { name: 'geometry' },
      // { name: 'multipoint' },
      // { name: 'multilinestring' },
      // { name: 'geometrycollection' }
    ]
    return this.typelist;
  }

  //小数点控制
  public typeXsd = ['double' ,'float','decimal','numeric'];
  //长度控制
  public typeLength = ['char', 'varchar','tinyblob','tinytext','blob','text','mediumblob','mediumtext','longblob','longtext'];

  //导航点击，获取栏目列表
  public access(opType, tableId, tables: any, tablesColRelations: any) {
    let url = this.server.baseUrl + 'Service/TalbesService/optTables';
    let data = {
      opType: opType,
      tableId: tableId,
      tables: tables,
      tablesColRelations: tablesColRelations
    }
    //console.log(data, url)
    return this.http.post(url, data).toPromise().then(res => res.json()).catch(err => err);
  }

  public typelist: any;

  //上移下移
  public upDown(val, i, terminals) {
    //console.log(terminals)
    if (val == 0) {
      //up
      if (i == 0) {
        return;
      }
      this.swapItems(terminals, i, i - 1);
    } else {
      //down
      if (i == terminals.length - 1) {
        return;
      }
      this.swapItems(terminals, i, i + 1);
    }
  }

  public swapItems(arr, index1, index2) {
    arr[index1] = arr.splice(index2, 1, arr[index1])[0];
    return arr;
  }

  public terminals: Array<tablestructureDatas> = [this.getTerminalTemplate()];

  //初始化数据
  public initData(){
    this.terminals.splice(0,this.terminals.length);
    this.addTr();
  }

  public getTerminalTemplate() {
    let terminalTemplate: tablestructureDatas = {
      columeXsdtf: 0,
      columeLengthtf:0,
      columnId: null,
      tableId: null,
      primaryKey: false,
      columeName: '',
      columeType: 'varchar',
      columeLength: null,
      columeXsd: 0,
      columeNotNull: true,
      columeOrder: 1,
      //columnDefault:'',
    }
    // return new SpecialResourceDatas(terminalTemplate);
    if(this.typeLength.indexOf (terminalTemplate.columeType) >= 0){
      terminalTemplate.columeLengthtf = 1;
    }else{
      terminalTemplate.columeLengthtf = 0;
    }
    return terminalTemplate
  }

  //添加一行
  public addTr() {
    this.terminals = [...this.terminals, this.getTerminalTemplate()];
  }

  //滑动块类型转换
  public mainArray() {
    for (let i in this.terminals) {
      this.terminals[i].columeNotNull = Number(this.terminals[i].columeNotNull);
      this.terminals[i].primaryKey = Number(this.terminals[i].primaryKey);
    }
  }

  //主键单选
  public primaryKeymin(i) {
    for (let k in this.terminals) {
      this.terminals[k].primaryKey = false;
    }
    if(this.terminals){
      this.terminals[i].primaryKey = true;
    }
    
  }

  //删除一行
  deletetr(i) {
    if (this.terminals.length == 1) {
      this.server.prompt = { prompt: true, success: false, fail: false, promptStatement: "最后一行了" };
      this.server.promptclose;
    } else {
      this.terminals.splice(i, 1);
    }
  }

}
